import logging
import os
import configparser


CFG = configparser.ConfigParser(
    interpolation=configparser.ExtendedInterpolation())
CFG.read(os.path.join(os.path.dirname(os.path.abspath(__file__)),
                      'config.ini'))
LOGGER_NAME = 'mispconn'


# Log
def setup_logger():
    result = logging.getLogger(LOGGER_NAME)
    result.setLevel(logging.DEBUG)
    fmt = logging.Formatter('%(asctime)s - %(process)d - %(filename)s '
                            '- %(levelname)s - %(message)s',
                            datefmt='%Y-%m-%d %H:%M:%S')
    level = 'DEBUG'
    f_handler = logging.FileHandler(CFG['log']['path'])
    f_handler.setLevel(level)
    f_handler.setFormatter(fmt)
    # console = logging.StreamHandler()
    # console.setLevel(level)
    # console.setFormatter(fmt)
    result.addHandler(f_handler)
    # result.addHandler(console)
    return result


def parse_excluding(file_paths_for_excluding):
    """Extract excluded items such as path or ID."""
    excluded_paths = set()
    if not file_paths_for_excluding:
        return excluded_paths
    elif isinstance(file_paths_for_excluding, str):
        file_paths_for_excluding = [file_paths_for_excluding]
    for p in file_paths_for_excluding:
        with open(p, 'r') as f:
            for line in f:
                excluded_paths.add(line.strip('\n'))
    return excluded_paths


if not logging.root.manager.loggerDict.get(LOGGER_NAME, False):
    logger = setup_logger()
else:
    logger = logging.getLogger(LOGGER_NAME)

